package com.jr9z.jrjz.controller;

import com.github.pagehelper.PageHelper;
import com.jr9z.jrjz.common.BaseResponse;
import com.jr9z.jrjz.common.ResponseBuilder;
import com.jr9z.jrjz.entity.Recruit;
import com.jr9z.jrjz.service.RecruitService;
import com.jr9z.jrjz.utils.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Author: MrZcc
 * @Description:招聘管理controller
 * @Date: Created in 15:13 2019-3-28
 * @Modified By:
 */

@Controller
@RequestMapping("/recruit")
public class RecruitController {


    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RecruitService recruitService;

    /**
     * 跳转到招聘管理
     *
     * @return
     */
    @RequestMapping("/recruitManage")
    public String recruitManage() {

        logger.info("进入招聘管理");

        return "/recruit/recruitManage";
    }

    /**
     * 跳转到招聘发布
     *
     * @return
     */
    @RequestMapping("/recruitPublish")
    public String recruitPublish() {

        logger.info("进入招聘发布");

        return "/recruit/recruitAdd";
    }

    /**
     * @Description:删除招聘信息
     * @date: 15:20 2019-3-28
     * @param: recruitId 招聘信息id
     * @return: com.jr9z.jrjz.common.BaseResponse
     */
    @RequestMapping(value = "/admin/deleteRecruit", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse deleteRecruitById(Integer recruitId) {

        int result = 0;
        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            result = recruitService.deleteByPrimaryKey(recruitId);
            custom = Constant.RESULT(result);
        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }
        return custom.build();
    }


    /**
     * @Description:获取招聘信息列表
     * @date: 15:34 2019-3-28
     * @param: currPage 当前页
     * @param: pageSize 每页显示数量
     * @return: 响应数据以json格式返回
     */
    @RequestMapping(value = "/getRecruitList", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getAllRecruit(Integer currPage, Integer pageSize) {

        ResponseBuilder custom = ResponseBuilder.custom();

        if (Objects.isNull(currPage) || currPage == 0) currPage = 1;
        if (Objects.isNull(pageSize) || pageSize == 0) pageSize = 10;
        //分页
        PageHelper.startPage(currPage, pageSize);

        try {
            List<Recruit> recruitList = recruitService.getAllRecruit();
            Long count = recruitService.getRecruitCount();

            if (recruitList != null && count != 0) {
                custom.data(recruitList).currPage(currPage).pageSize(pageSize).totalCount(count.intValue());
            }

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }

    /**
     * @Description:获取招聘信息列表
     * @date: 15:34 2019-3-28
     * @param: currPage 当前页
     * @param: pageSize 每页显示数量
     * @return: 响应数据以json格式返回
     */
    @RequestMapping(value = "/admin/getRecruitList", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse adminGetAllRecruit(Integer currPage, Integer pageSize) {
        ResponseBuilder custom = ResponseBuilder.custom();

        if (Objects.isNull(currPage) || currPage == 0) currPage = 1;
        if (Objects.isNull(pageSize) || pageSize == 0) pageSize = 10;
        //分页
        PageHelper.startPage(currPage, pageSize);

        try {
            List<Recruit> recruitList = recruitService.getAllRecruit();
            Long count = recruitService.getRecruitCount();

            if (recruitList != null && count != 0) {
                custom.data(recruitList).currPage(currPage).pageSize(pageSize).totalCount(count.intValue());
            }

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }


    /**
     * @Description:查询单条招聘信息
     * @date: 15:51 2019-3-28
     * @param: recruitId  招聘信息id
     * @return: 查询结果以json格式返回
     */
    @RequestMapping(value = "/getRecruitById", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getRecruitById(Integer recruitId) {
        Recruit recruit;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            recruit = recruitService.selectByPrimaryKey(recruitId);

            //查询数据为空
            if (recruit == null) {
                custom.failed(Constant.FAIL, Constant.ERROR_CODE);
            } else {
                //查询成功，返回查到的信息
                custom.data(recruit).success(Constant.SUCCESS, Constant.SUCCESS_CODE);
            }
        } catch (Exception e) {
            System.out.println(e.toString());
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }
        return custom.build();
    }

    /**
     * @Description:查询单条招聘信息
     * @date: 15:51 2019-3-28
     * @param: recruitId  招聘信息id
     * @return: 查询结果以json格式返回
     */
    @RequestMapping(value = "/admin/getRecruitById", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse adminGetRecruitById(Integer recruitId) {
        Recruit recruit;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            recruit = recruitService.selectByPrimaryKey(recruitId);

            //查询数据为空
            if (recruit == null) {
                custom.failed(Constant.FAIL, Constant.ERROR_CODE);
            } else {
                //查询成功，返回查到的信息
                custom.data(recruit).success(Constant.SUCCESS, Constant.SUCCESS_CODE);
            }
        } catch (Exception e) {
            System.out.println(e.toString());
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }
        return custom.build();
    }


    /**
     * @Description:更新招聘信息
     * @date: 16:13 2019-3-28
     * @param: recruit json格式的recruit实体
     * @return: com.jr9z.jrjz.common.BaseResponse
     */
    @RequestMapping(value = "/admin/updateRecruitById", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse updateRecruitById(@RequestBody Recruit recruit) {
        int result = 0;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            //更新招聘信息
            result = recruitService.updateByPrimaryKeySelective(recruit);

            //结果判断处理
            custom = Constant.RESULT(result);
        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }
        return custom.build();
    }

    /**
     * @Description:添加招聘信息
     * @date: 16:19 2019-3-28
     * @param: recruit json格式的招聘信息实体
     * @return: com.jr9z.jrjz.common.BaseResponse
     */
    @RequestMapping(value = "/admin/addRecruit", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse addRecruit(@RequestBody Recruit recruit) {
        int result = 0;

        ResponseBuilder custom = ResponseBuilder.custom();
        try {
            result = recruitService.insertSelective(recruit);

            //结果判断处理
            custom = Constant.RESULT(result);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }

}
